package com.chandra.pos.payment.service.impl;

import com.chandra.pos.common.model.TransactionType;
import com.chandra.pos.customer.dao.entity.TransactionEntity;
import com.chandra.pos.customer.model.CustomerFilter;
import com.chandra.pos.customer.model.CustomerModel;
import com.chandra.pos.customer.service.CustomerRole;
import com.chandra.pos.customer.service.TransactionRole;
import com.chandra.pos.payment.model.PaymentFilter;
import com.chandra.pos.payment.model.PaymentList;
import com.chandra.pos.payment.model.PaymentModel;
import com.chandra.pos.payment.service.PaymentRole;
import com.chandra.pos.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * Created by dnagarajan on 3/28/2015.
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentRole paymentRole;

    @Autowired
    private CustomerRole customerRole;


    @Autowired
    TransactionRole transactionRole;



    @Override
    public PaymentList search(PaymentFilter model) {
        return paymentRole.search(model);
    }


    @Override
    @Transactional
    public PaymentModel saveCustomerPayment(PaymentModel paymentModel) {

        paymentModel= paymentRole.savePayment(paymentModel);
        CustomerModel customerModel= customerRole.getCustomerDetails(new CustomerFilter(paymentModel.getCustomerId()));
        customerModel.setBalanceAmount((customerModel.getBalanceAmount()==null?0d:customerModel.getBalanceAmount()) -paymentModel.getPaymentAmount());


        customerRole.saveCustomer(customerModel);

        TransactionEntity transactionEntity =new TransactionEntity();
        transactionEntity.setCustomerId(paymentModel.getCustomerId());
        transactionEntity.setTransactionType(TransactionType.PT);
        transactionEntity.setTransactionAmount(paymentModel.getPaymentAmount());
        transactionEntity.setTransactionDetailId(paymentModel.getPaymentId());
        transactionEntity.setTransactionDate(paymentModel.getPaymentDate());
        transactionEntity.setCustomerName(customerModel.getName());


        transactionRole.saveTransaction(transactionEntity);


        return paymentModel;
    }

    @Override
    @Transactional
    public PaymentModel saveVendorPayment(PaymentModel paymentModel) {

        paymentModel= paymentRole.savePayment(paymentModel);
        CustomerModel customerModel= customerRole.getCustomerDetails(new CustomerFilter(paymentModel.getCustomerId()));
        customerModel.setBalanceAmount(customerModel.getBalanceAmount()==null?0d:customerModel.getBalanceAmount() + paymentModel.getPaymentAmount());
        customerRole.saveCustomer(customerModel);

        TransactionEntity transactionEntity =new TransactionEntity();
        transactionEntity.setCustomerId(paymentModel.getCustomerId());
        transactionEntity.setTransactionType(TransactionType.BP);
        transactionEntity.setTransactionAmount(paymentModel.getPaymentAmount());
        transactionEntity.setTransactionDetailId(paymentModel.getPaymentId());
        transactionEntity.setTransactionDate(paymentModel.getPaymentDate());
        transactionEntity.setCustomerName(customerModel.getName());

        transactionRole.saveTransaction(transactionEntity);


        return paymentModel;
    }

    @Override
    @Transactional
    public PaymentModel saveCustomerRefund(PaymentModel paymentModel) {

        paymentModel= paymentRole.savePayment(paymentModel);

        TransactionEntity transactionEntity =new TransactionEntity();
        transactionEntity.setCustomerId(paymentModel.getCustomerId());
        transactionEntity.setTransactionType(TransactionType.CM);
        transactionEntity.setTransactionAmount(paymentModel.getPaymentAmount());
        transactionEntity.setTransactionDetailId(paymentModel.getPaymentId());
        transactionEntity.setTransactionDate(paymentModel.getPaymentDate());

        transactionRole.saveTransaction(transactionEntity);


        return paymentModel;
    }

}
